﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Belyn.Common.Xml;
using Cartoon.Model;
using System.Xml;

namespace Cartoon.Config
{
    public class ConfigForCartoon : ICartoon
    {
        protected static ConfigForCartoon _instance;
        protected IXmlOperate xmlOp;
        private ConfigForCartoon(string _configPath)
        {
            this.configPath = _configPath;
            xmlOp = new ConfigOperate(_configPath);

        }
        protected string configPath;

        public string Domain
        {
            get
            {
                return this.xmlOp.Read("Common/Domain").Trim();
            }
        }

        public string PictureDomain
        {
            get
            {
                return this.xmlOp.Read("Common/PictureDomain").Trim();
            }
        }

        public string SavePath
        {
            get
            {
                return this.xmlOp.Read("Common/SavePath").Trim();
            }
            set  
            {
                this.xmlOp.Write("Common/SavePath", value);
            }
        }

        public string FirstUse
        {
            get
            {
                return this.xmlOp.Read("Common/FirstUse").Trim();
            }
            set
            {
                this.xmlOp.Write("Common/FirstUse", value);
            }
        }

        /// <summary>
        /// 加载默认配置文件 belyn-cartoon-cfg.xml
        /// </summary>
        /// <returns></returns>
        public static ConfigForCartoon CreateInstance()
        {
            return CreateInstance("belyn-cartoon-cfg.xml");
        }

        public static ConfigForCartoon CreateInstance(string _configPath)
        {
            
             _instance = new ConfigForCartoon(_configPath);
            

            return _instance;
        }

        #region ICartoon 成员

        public List<Cartoon.Model.Comic> LoadCartoonList()
        {
            List<Comic> cartoonList = new List<Comic>();

            System.Xml.XmlNodeList cartoonNodeList = this.xmlOp.GetNodeList("Cartoons/Cartoon");

            if (cartoonNodeList != null)
            {
                foreach (XmlNode node in cartoonNodeList)
                {
                    Comic comic = new Comic();
                    comic.Id = int.Parse(node.SelectSingleNode("Id").InnerText);
                    comic.Name = node.SelectSingleNode("Name").InnerText.Trim();
                    comic.Url = node.SelectSingleNode("Url").InnerText.Trim();
                    comic.RegexExpForChapter = node.SelectSingleNode("RegexExpForChapter").InnerText.Trim();
                    comic.RegexExpForPicList = node.SelectSingleNode("RegexExpForPicList").InnerText.Trim();
                    comic.PictureDomain = node.SelectSingleNode("PictureDomain").InnerText.Trim();
                    comic.PictureUrl = node.SelectSingleNode("PictureUrl").InnerText.Trim();
                    comic.ChapterDomain = node.SelectSingleNode("ChapterDomain").InnerText.Trim();

                    cartoonList.Add(comic);
                }
            } 

            return cartoonList; 
            
        }

        #endregion
    }
}
